﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Chemical.BLL;
using Yipusoft.Chemical.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;

public partial class BasicBase_Chemical_ChemicalNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        List<string> list = Chemical.GetAllChemicalClass();
        ChemicalClass.Items.Add("");
        foreach (string item in list)
        {
            ChemicalClass.Items.Add(item);
        }
        if (Request.QueryString["ID"] == null)
        {
            #region 权限
            bool isPermission = false;
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201600"))
            {
                isPermission = true;
            }
            else
            {

                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201603"))
                {
                    isPermission = true;
                }

            }
            if (!isPermission)
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            #endregion
            caption.Text = "添加化学危险品";
            lbkWordView.Visible = false;
        }
        else
        {
            #region 权限
            bool isPermission = false;
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201600"))
            {
                isPermission = true;
            }
            else
            {

                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201604"))
                {
                    isPermission = true;
                }

            }
            if (!isPermission)
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            #endregion
            caption.Text = "修改化学危险品";
            lbkWordView.Visible = true;

            Guid ID = new Guid(Request.QueryString["ID"]);
            ChemicalInfo info = Chemical.getChemicalByID(ID);
            ChemicalClass.SelectedValue = info.ChemicalClass;
            CnName.Text = info.CnName;
            EnName.Text = info.EnName;
            OtName.Text = info.OtName;
            ReferenceChemical.Text = info.ReferenceChemical;
            ReferenceScheme.Text = info.ReferenceScheme;
            LD50.Text = info.LD50;
            LC50.Text = info.LC50;
            //气味
            setCheckList(info.Smell, Smell);
            //颜色
            setCheckList(info.Color, Color);
            //状态
            setCheckList(info.Status, Status);
            //危险性
            setCheckList(info.Danger, Danger);
            //燃烧分解产物
            setCheckList(info.Resolvent, Resolvent);
            //储存包装
            setCheckList(info.Package, Package);
            //禁忌物
            setCheckList(info.Avoid, Avoid);
            //皮肤
            setCheckList(info.Skin, Skin);
            //眼
            setCheckList(info.Eye, Eye);
            //呼吸道
            setCheckList(info.RespiratoryTract, RespiratoryTract);
            //窒息症状
            setCheckList(info.Stifle, Stifle);
            //中枢神经系统
            setCheckList(info.Nervous, Nervous);
            //呼吸系统
            setCheckList(info.RespiratorySystem, RespiratorySystem);
            setCheckList(info.DigestiveSystem, DigestiveSystem);
            setCheckList(info.CirculatorySystem, CirculatorySystem);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        byte[] word = new byte[] { };
        if (WordFile.HasFile)
        {
            string ex = WordFile.FileName.Substring(WordFile.FileName.LastIndexOf('.') + 1);
            if (ex.ToLower() != "doc")
            {
                Prompt.Text = "提示:上传必须为Word文档!";
                return;
            }
            else
            {
                word = WordFile.FileBytes;
            }
        }

        ChemicalInfo info = new ChemicalInfo();
        if (Request.QueryString["ID"] == null)
        {

        }
        else
        {
            info = Chemical.getChemicalByID(new Guid(Request.QueryString["ID"]));
        }
        info.Avoid = Avoid.Text;
        info.CanEdit = true;
        info.ChemicalClass = ChemicalClass.SelectedValue;
        info.CirculatorySystem = GetCheckList(CirculatorySystem);
        info.CnName = CnName.Text;
        info.Color = GetCheckList(Color);
        info.Danger = GetCheckList(Danger);
        info.DepartmentID = userInfo.DepartmentID;
        info.DigestiveSystem = GetCheckList(DigestiveSystem);
        info.EnName = EnName.Text;
        info.Eye = GetCheckList(Eye);
        info.LC50 = LC50.Text;
        info.LD50 = LD50.Text;
        info.Nervous = GetCheckList(Nervous);
        info.OtName = OtName.Text;
        info.Package = GetCheckList(Package);
        info.ReferenceChemical = ReferenceChemical.Text;
        info.ReferenceScheme = ReferenceScheme.Text;
        info.Resolvent = GetCheckList(Resolvent);
        info.RespiratorySystem = GetCheckList(RespiratorySystem);
        info.RespiratoryTract = GetCheckList(RespiratoryTract);
        info.SetAID = userInfo.UserID;
        info.SetTime = DateTime.Now;
        info.Skin = GetCheckList(Skin);
        info.Smell = GetCheckList(Smell);
        info.Status = GetCheckList(Status);
        info.Stifle = GetCheckList(Stifle);
        if (word.Length > 0)
        {
            info.WordFile = word;
        }

        if (Request.QueryString["ID"] == null)
        {
            //添加
            Guid id = Chemical.Insert(info);
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "添加一个化危品";
            log.ItemID = id.ToString();
            log.ItemName = "BasicBase";
            log.ItemFullUrl = "Chemical/ChemicalNew.aspx";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);
            Prompt.Text = "提示:添加成功!";
        }
        else
        {
            //修改
            Chemical.Update(info);
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "添加一个化危品";
            log.ItemID = Request.QueryString["ID"];
            log.ItemName = "BasicBase";
            log.ItemFullUrl = "Chemical/ChemicalNew";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);
            Prompt.Text = "提示:修改成功!";
        }

    }

    void setCheckList(string Name, CheckBoxList cbl)
    {
        string[] items = Name.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        foreach (ListItem item in cbl.Items)
        {
            foreach (string name in items)
            {
                if (item.Text == name)
                {
                    item.Selected = true;
                }
            }
        }
    }

    string GetCheckList(CheckBoxList cbl)
    {
        string strContion = "";
        foreach (ListItem item in cbl.Items)
        {
            if (item.Selected)
            {
                strContion += item.Text + ",";
            }
        }

        return strContion;
    }
}
